package me.zxk.qwert_poetry.service;

import me.zxk.qwert_poetry.dao.Collection;
import me.zxk.qwert_poetry.dao.CollectionRepository;
import me.zxk.qwert_poetry.dao.Poem;
import me.zxk.qwert_poetry.dao.PoemRepository;
import me.zxk.qwert_poetry.util.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionRepository collectionRepository;

    @Autowired
    private PoemRepository poemRepository;

    private final int ERR_COLLECTION_NOT_FOUND = 1000;
    private final int ERR_POEM_NOT_FOUND = 1001;

    @Override
    public Collection getCollectionById(int id) {
        return collectionRepository.findById(id)
                .orElseThrow(() -> new ApiException("Collection does not exist", ERR_COLLECTION_NOT_FOUND));
    }

    @Override
    public Collection createCollection(Collection collection) {
        return collectionRepository.save(collection);
    }

    @Override
    public void deleteCollection(int id) {
        Collection collection = collectionRepository.findById(id).orElseThrow(() -> new ApiException("Collection does not exist", ERR_COLLECTION_NOT_FOUND));
        collectionRepository.delete(collection);
    }

    @Override
    public void deleteCollectionById(int id) {
        Collection collection = collectionRepository.findById(id).orElse(null);
        if (collection != null) {
            collectionRepository.deleteById(id);
        } else {
            throw new ApiException("Collection does not exist", ERR_COLLECTION_NOT_FOUND);
        }
    }

    @Override
    public List<Collection> listCollectionsByUserId(int userId) {
        return collectionRepository.findAllByUserId(userId);
    }

    @Override
    public Collection editCollection(Collection collection) {
        Collection collectionInDb = collectionRepository.findById(collection.getId()).orElse(null);
        if (collectionInDb != null) {
            collectionInDb.setTitle(collection.getTitle());
            collectionInDb.setDesc(collection.getDesc());
            collectionRepository.save(collectionInDb);
        } else {
            throw new ApiException("Collection does not exist", ERR_COLLECTION_NOT_FOUND);
        }
        return collectionInDb;
    }

    @Override
    public void collect(Integer collectionId, Integer poemId) {
        Collection collection = collectionRepository.findById(collectionId)
                .orElseThrow(() -> new ApiException("Collection does not exist", ERR_COLLECTION_NOT_FOUND));
        Poem poem = poemRepository.findById(poemId)
                .orElseThrow(() -> new ApiException("Poem does not exist", ERR_POEM_NOT_FOUND));
        if (!collection.getPoems().contains(poem)) {
            collection.getPoems().add(poem);
            collectionRepository.save(collection);
        }
    }

    @Override
    public void cancel(Integer collectionId, Integer poemId) {
        Collection collection = collectionRepository.findById(collectionId)
                .orElseThrow(() -> new ApiException("Collection does not exist", ERR_COLLECTION_NOT_FOUND));
        Poem poem = poemRepository.findById(poemId)
                .orElseThrow(() -> new ApiException("Poem does not exist", ERR_POEM_NOT_FOUND));
        if (collection.getPoems().contains(poem)) {
            collection.getPoems().remove(poem);
            collectionRepository.save(collection);
        }
    }
}
